package com.example.databindingdemo.viewmodel;

import android.util.Log;

import androidx.databinding.BaseObservable;
import androidx.databinding.ObservableField;

/**
 * @author: njb
 * @date: 2022/9/18 16:42
 * @desc: 使用ObservableField优化双向绑定
 */
public class TwoWayBindingViewModelField extends BaseObservable {
    private ObservableField<LoginModel> loginModelObservableField;

    public TwoWayBindingViewModelField() {
        LoginModel loginModel = new LoginModel();
        loginModel.userName = "Jack";
        loginModelObservableField = new ObservableField<>();
        loginModelObservableField.set(loginModel);
    }

    public void setUserName(String userName) {
        loginModelObservableField.get().userName = userName;
        Log.d("--fieldViewModel--", userName);
    }

    public String getUserName() {
        return loginModelObservableField.get().userName;
    }
}
